﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace GqqCodeFirstWithSqlserver.Products
{
    public class ProductProcess
    {
        public static void DoTest()
        {
            Console.WriteLine("please input key:1、addproduct 2、...");
            var key = Console.ReadLine();
            switch (key)
            {
                case "1":
                    AddProducts();
                    DisplayProducts();
                    break;
                default:
                    DisplayProducts();
                    break;
            }
        }

        public static void GetFirstProductCategory()
        {
            using (var db = new ProductsContext())
            {
                var category = db.Products.FirstOrDefault().Category;
                Console.WriteLine(category);
            }
        }



        public static void AddProducts(bool isInit=false)
        {
            using (var ctx = new ProductsContext())
            {
                if (!ctx.Products.Any() && isInit)
                {
                    ctx.Products.Add(new Product()
                    {
                        ProductName = "Northwest Milk",
                        Category = ProductCategory.Dairy
                    });
                    ctx.Products.Add(new Product()
                    {
                        ProductName = "Tacoma's Cayenne Pepper",
                        Category = ProductCategory.Condiments
                    });
                    ctx.Products.Add(new Product()
                    {
                        ProductName = "Sour Cream",
                        Category = ProductCategory.Dairy
                    });
                    ctx.SaveChanges();
                }
                else
                {
                    Console.WriteLine("Please input product name:");
                    var productName = Console.ReadLine();
                    Console.WriteLine("Please input category name:");
                    ProductCategory catogory;
                    if (!Enum.TryParse<ProductCategory>(Console.ReadLine(), out catogory))
                        catogory = ProductCategory.Produce;
                    ctx.Products.Add(new Product { Category = catogory, ProductName = productName });
                    ctx.SaveChanges();
                }

            }
        }

        public static void DisplayProducts()
        {
            using (var db = new ProductsContext())
            {
                DisplayProducts(db.Products);
            }
        }

        private static void DisplayProducts(IEnumerable<Product> products)
        {
            Console.WriteLine("Products");
            foreach (var p in products)
            {
                Console.WriteLine("Id: {0}, Name: {1}, Category: {2}", p.Id, p.ProductName, p.Category);
            }
        }
    }
}
